import numpy as np
from scipy.stats import norm

# 参数设置
S = 90      # 股票价格
K = 95      # 执行价
r = 0.06    # 无风险利率
T = 0.5     # 到期时间（年）

# 定义计算函数
def black_scholes_put(S, K, r, sigma, T):
    d1 = (np.log(S/K) + (r + 0.5*sigma**2)*T) / (sigma * np.sqrt(T))
    d2 = d1 - sigma * np.sqrt(T)
    put_price = K * np.exp(-r*T) * norm.cdf(-d2) - S * norm.cdf(-d1)
    return put_price

def black_scholes_call(S, K, r, sigma, T):
    d1 = (np.log(S/K) + (r + 0.5*sigma**2)*T) / (sigma * np.sqrt(T))
    d2 = d1 - sigma * np.sqrt(T)
    call_price = S * norm.cdf(d1) - K * np.exp(-r*T) * norm.cdf(d2)
    return call_price

# 问题1：计算看跌期权价格 (sigma = 25%)
sigma1 = 0.25
P1 = black_scholes_put(S, K, r, sigma1, T)
print(f"看跌期权价格 (sigma=25%): {P1:.4f} 元")

# 问题2：验证看涨-看跌平价
C1 = black_scholes_call(S, K, r, sigma1, T)
put_call_parity = C1 - P1
rhs = S - K * np.exp(-r*T)
print(f"看涨-看跌平价 C-P: {put_call_parity:.4f}")
print(f"S - K*e^(-rT): {rhs:.4f}")

# 问题3：波动率提高到35%
sigma2 = 0.35
P2 = black_scholes_put(S, K, r, sigma2, T)
print(f"看跌期权价格 (sigma=35%): {P2:.4f} 元")
print(f"波动率上升，看跌期权价值增加: {P2 - P1:.4f} 元")

